package com.example.wechat;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;


public class ReciverActivity extends AppCompatActivity {
     Button button;
     MyReceiver2 myReceiver2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reciver);
        String[] permission={Manifest.permission.RECEIVE_SMS};
        int requetCode=000;
        String actionname="jd.broadcast.000";
        button=findViewById(R.id.buttonb);
        Intent intent=new Intent(actionname);
        myReceiver2=new MyReceiver2();

        IntentFilter filter=new IntentFilter();
        filter.addAction(actionname);
        registerReceiver(myReceiver2,filter);
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {//定时器，在多长时间发送一次
            @Override
            public void run() {
                sendBroadcast(intent);
            }
        },30);


        ActivityCompat.requestPermissions(this,permission,requetCode);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendBroadcast(intent);
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 000) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            } else {
            }
        }
        else if(requestCode==2){

        }
    }
}